package com.shouxin.attendance.activity;

import android.content.Intent;
import android.text.TextUtils;

import com.shouxin.attendance.R;
import com.shouxin.attendance.constants.PrefKey;
import com.shouxin.attendance.database.DBHelper;
import com.shouxin.attendance.utils.SharedPref;

import org.apache.log4j.Logger;

public class LoadActivity extends BaseActivity {

	private final Logger logger = Logger.getLogger(LoadActivity.class);

	@Override
	public void setContentView() {
		setContentView(R.layout.activity_load);
	}

	@Override
	public void initData() {
		String account = SharedPref.getString(PrefKey.ACCOUNT, "");
		String pwd = SharedPref.getString(PrefKey.PASSWORD, "");
		if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(pwd)) {
			// 创建数据库
			DBHelper.getInstance().initDB(this, account);

			gotoMain();
		} else {
			gotoLogin();
		}
	}

	@Override
	public void initUIViews() {

	}

	@Override
	public void bindEvents() {

	}

	private void gotoMain() {
		logger.debug("gotoMain...");
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				startActivity(new Intent(LoadActivity.this, MainActivity.class));
				finish();
			}
		}, 2000);
	}

	private void gotoLogin() {
		logger.debug("gotoLogin...");
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				startActivity(new Intent(LoadActivity.this, LoginActivity.class));
				finish();
			}
		}, 2000);
	}
}
